package org.dk.shiro;

import com.alibaba.dubbo.config.annotation.Reference;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.dk.pojo.TbUser;
import org.dk.service.UserService;

import java.util.Objects;

public class MyRealm extends AuthorizingRealm {

   @Reference
   private UserService userService;

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {

        return null;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        UsernamePasswordToken token = (UsernamePasswordToken)authenticationToken;
        TbUser tbUser = userService.selectByName(token.getUsername());
        if(tbUser == null){
            return null;
        }
        //校验密码 交由shiro来帮我们校验   1.登录成功后的对象 2.数据库中的密码 3.数据库中加密的盐 4.用户名称
        return new SimpleAuthenticationInfo(tbUser,tbUser.getPassword(),
                ByteSource.Util.bytes(tbUser.getSalt()),tbUser.getUsername());

    }
}
